package com.webchat.aigc.service.transfunction;


import com.webchat.common.enums.bot.TransFunctionEnum;
import com.webchat.common.exception.BusinessException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class TransFunctionFactory implements InitializingBean, ApplicationContextAware {

    private ApplicationContext applicationContext;

    private static Map<String, AbstractTransFuncService> services = new HashMap<>();

    @Override
    public void afterPropertiesSet() throws Exception {
        this.registryService();
    }

    /**
     * 根据账号角色注册服务bean
     *
     */
    private void registryService() {

        services.put(TransFunctionEnum.USERNAME_2_USERID.getTransFunction(), applicationContext.getBean(UserName2UserIdFunc.class));
    }

    public static AbstractTransFuncService getService(String ruleName) {
        AbstractTransFuncService transFuncService = services.get(ruleName);
        if (transFuncService != null) {
            return transFuncService;
        }
        throw new BusinessException("不支持的Trans函数:" + ruleName);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
